import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;

import unilearn.da.DABase;
import unilearn.domain.Assessment;
import unilearn.domain.Assignment;
import unilearn.domain.Submission;
import unilearn.facade.AssessmentFacade;
import unilearn.facade.AssignmentFacade;
import unilearn.facade.SubmissionFacade;

public class UseStories2
{
	private PrintWriter out;

	public UseStories2() throws SQLException, FileNotFoundException
	{
		out = new PrintWriter(new FileOutputStream("UserStories2.txt"));
	}

	public void close()
	{
		try
		{
			out.close();
		}
		finally
		{
			out = null;
		}
	}

	public void printStudents() throws SQLException
	{
		String[] students = SubmissionFacade.getStudentNumbers();
		for (String sub : students)
		{
			out.println(sub);
		}
	}

	public void printSubmissions(String studentNumber) throws SQLException
	{
		out.print("Student ID: ");
		out.println(studentNumber);
		out.println();

		Submission[] submissions = SubmissionFacade.getSubmissions(studentNumber);
		for (Submission sub : submissions)
		{
			printSubmission(sub);
		}

		out.println();
	}

	private void printSubmission(Submission sub) throws SQLException
	{
		Assignment ass;
		ass = AssignmentFacade.getAssignmentBySubmission(sub);
		long subjectID = ass.getAssignmentSubjectID();

		out.print("<Subject (");
		out.print(subjectID);

		out.print(")> | ");

		out.print(ass.getAssignmentName());

		out.print(" | Grade: ");
		Assessment ases = AssessmentFacade.getSubmissionAssessment(sub);
		if (ases == null)
		{
			out.println("UN-ASSESSED");
		}
		else
		{
			out.println(ases.getGrade());
		}
	}

	public static void main(String[] args) throws SQLException, FileNotFoundException
	{
		UseStories2 test = new UseStories2();
		try
		{
			if (args.length != 1)
			{
				test.printStudents();
			}
			else
			{
				test.printSubmissions(args[0]);
			}
		}
		finally
		{
			DABase.shutdown();
			test.close();
		}

	}
}
